package com.student.liu.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.student.liu.entity.Goods;
import com.student.liu.service.IGoodsService;
import com.student.liu.service.IGoodsTypeService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.Random;

/**
 * 商品 Controller
 */
@RestController
@RefreshScope
public class GoodsController extends BaseController {

    @Resource
    IGoodsService goodsService;
    @Resource
    IGoodsTypeService goodsTypeService;
    @Value("${ipBlackList}")
    String ipBlackList;

    /**
     * 搜索
     * @return
     */
    @PostMapping("search")
    public Object search(@RequestBody String searchStr) {
        return success(null, goodsService.search(searchStr));
    }

    @PostMapping("page")
    @SentinelResource(
            value = "goodsPage",            // 用于定义资源名称
            fallback = "pageFallback"       // 用于定义失败之后的降级处理
    )
    public Object page(@Valid @RequestBody GoodsPageBody goodsPageBody, BindingResult result) throws InterruptedException {
        //生成随机数
//        Random random = new Random();
//        int time = random.nextInt(5000);
//        Thread.sleep(time); //让程序暂停5s， 用于模拟慢处理
        IPage<Map<String, Object>> goodsMaps = goodsService.goodsPage(goodsPageBody.getPageIndex(), 15,
                goodsPageBody.getTypeId(), 1 );
        return success("", goodsMaps);
    }

    /**
     * page 接口请求失败后的降级处理
     * @param goodsPageBody
     * @param result
     * @return
     */
    public Object pageFallback(@Valid @RequestBody GoodsPageBody goodsPageBody, BindingResult result) {
        return fail("请求失败，请稍后重试！", 429);
    }


    /**
     * 厨师查询
     * @param goodsPageBody
     * @param result
     * @return
     * @throws InterruptedException
     */
    @PostMapping("cookbook")
    public Object cookbook(@Valid @RequestBody GoodsPageBody goodsPageBody, BindingResult result) throws InterruptedException {
        IPage<Map<String, Object>> goodsMaps = goodsService.cookbook(goodsPageBody.getPageIndex(), 15,
                goodsPageBody.getTypeId(), goodsPageBody.getStateId(),goodsPageBody.getName());
        return success("", goodsMaps);
    }



    @GetMapping("get/{id}")
    @SentinelResource(value = "goodsGetById")
    public Object get(@PathVariable Integer id) {
        return success(null, goodsService.goodsGetById(id));
    }


    /**
     * 创建一个新商品
     *
     * @param goods
     * @return
     */
    @PostMapping("create")
    @PreAuthorize("hasRole('ROLE_cook')")
    public Object create(@RequestBody Goods goods) {
        goodsService.createGoods(goods);
        return success("创建成功", null);
    }

    /**
     * 上架一个商品
     *
     * @return
     */
    @GetMapping("putOn/{id}")
    public Object putOn(@PathVariable Integer id) {
        goodsService.putOn(id);
        return success("上架成功", null);
    }

    /**
     * 下架一个商品
     *
     * @return
     */
    @GetMapping("putDown/{id}")
    public Object putDown(@PathVariable Integer id) {
        goodsService.putDown(id);
        return success("下架成功", null);
    }

    @GetMapping("test")
    public Object test() {
        return success(null, ipBlackList);
    }

    /**
     * 商品分页查询参数实体
     */
    @Data
    static class GoodsPageBody {
        @NotNull(message = "请传递页码")
        private Integer pageIndex;
        @NotNull(message = "请传递类型，默认请传递0")
        private Integer typeId;
        private Integer stateId;
        private String name;
    }


}
